<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');


/**
 * This class is for online shop order process - payment.
 * params order step
 * This is the children of controller.
 * PHP5 CMS Typolight
 * Copyright is 34n.sam@gmail.com
 */
 
 /**
 * Start page
 */
session_start();

$shipping = new ShippingPayment();
$shipping->run();

class ShippingPayment extends Controller
{	
	/**
	 * This is class construct function.
	 * @params null
	 * @Return null
	 */
	public function __construct()
	{
		parent::__construct();		
	}
	
	/** 
	 * Start page function
	 * @params null
	 * @Return null
	 */
	public function run()
	{
		$this->import('Database');
		
		echo $this->displayShippingPayment();
	}
	
	/**
	 * Generate the shipping payment html
	 * @params null
	 * @Return string
	 */
	private function displayShippingPayment()
	{
		$arrAddr = array();
		$arr_countries = array();
		
		$this->loadLanguageFile('countries');
		include(TL_ROOT . '/system/config/countries.php');
		$obj_countries = $this->Database->prepare("SELECT * FROM `tl_countries` ORDER BY order_num,name")
										->execute(time());
		while ($obj_countries->next())
		{
			if ($obj_countries->order_num != 999)
			{
				$arr_countries[$obj_countries->name] = $countries[$obj_countries->name];
				unset($countries[$obj_countries->name]);
			}
		}
		asort($countries);
		$countries = array_merge($arr_countries, $countries);
		
		if (FE_USER_LOGGED_IN)
		{
			$objSession = $this->Database->prepare("SELECT * FROM tl_session WHERE hash=? AND name=?")
									 ->execute($_COOKIE['FE_USER_AUTH'], 'FE_USER_AUTH');
			
			if ($objSession->numRows < 1)
				return;
			
			/** 
			 * Get default address of shipping
			 */
			$index = 0;
			/**
			 * Get default address of billing
			 */
			$objMember = $this->Database->prepare("SELECT * FROM tl_member WHERE id=?")
										->execute($objSession->pid);
			
			
			if ($objMember->numRows < 1)
				return;				
			
			$_SESSION['ORDER']['mem_id'] = $objMember->id;
			$arrCitys = array();
			
			// Get the states.
			$arrStates = array();
			$objStates = $this->Database->prepare("SELECT * FROM `china_province` ORDER BY `province_en`")
										->execute(time());
			
			while ($objStates->next())
			{
				$arrStates[$objStates->id] = $objStates->province_en;
			}
			
			// Get State.
			$arrCitys = array();			
			$objCitys = $this->Database->prepare("SELECT * FROM `china_city` ORDER BY `city_en`")
										->execute(time());
			
			while ($objCitys->next())
			{
				$arrCitys[$objCitys->id] = stripslashes($objCitys->city_en);
			}
			
			$objAddressBook = $this->Database->prepare("SELECT * FROM tl_address_book WHERE uid=?")
									->execute($objSession->pid);			
			if ($objAddressBook->numRows < 1)
			{
				/*$arrAddr[$index]['uid'] = $objMember->id;
				$arrAddr[$index]['username'] = $objMember->firstname . (($GLOBALS['TL_LANGUAGE'] == 'en') ?  ' ' : '') . $objMember->lastname;
				$arrAddr[$index]['firstname'] = $objMember->firstname;
				$arrAddr[$index]['lastname'] = $objMember->lastname;
				$arrAddr[$index]['address'] = $objMember->street;
				$arrAddr[$index]['street'] = $objMember->street;
				$arrAddr[$index]['postcode'] = $objMember->postal;
				$arrAddr[$index]['state'] = $objMember->state;
				$arrAddr[$index]['country'] = $countries[$objMember->country];
				$arrAddr[$index]['id_country'] = $objMember->country;
				$arrAddr[$index]['city'] = $objMember->city;
				$arrAddr[$index]['cellphone'] = $objMember->mobile;
				$arrAddr[$index]['telephone'] = $objMember->phone;
				
				// Insert into address book.
				$insertId = $this->Database->prepare("INSERT INTO tl_address_book (`tstamp`, `uid`, `gender`, `firstname`, `lastname`, `street`, `city`, `state`, `country`, `postcode`, `telephone`, `cellphone`, `email`, `street_2`, `default_address`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
								->execute(time(), $objSession->pid, $objMember->gender, $objMember->firstname, $objMember->lastname, $objMember->street, $objMember->city, $objMember->state, $objMember->country, $objMember->postal, $objMember->phone, $objMember->mobile, $objMember->email, '', 1);
				
				$arrAddr[$index]['id'] = $insertId->insertId;*/
			}
			else
			{
				
				while ($objAddressBook->next())
				{
					$arrAddr[$index]['id'] = $objAddressBook->id;
					$arrAddr[$index]['uid'] = $objAddressBook->uid;
					$arrAddr[$index]['username'] = $objAddressBook->firstname . (($GLOBALS['TL_LANGUAGE'] == 'en') ?  ' ' : '') . $objAddressBook->lastname;
					$arrAddr[$index]['firstname'] = $objAddressBook->firstname;
					$arrAddr[$index]['lastname'] = $objAddressBook->lastname;
					if($objAddressBook->street_2=="line 2" || $objAddressBook->street_2=="Address line 2") $objAddressBook->street_2="";
					$arrAddr[$index]['address'] = $objAddressBook->street . (($GLOBALS['TL_LANGUAGE'] == 'en') ?  ' ' : '') . (($objAddressBook->street_2) ? ('<br />' . $objAddressBook->street_2) : '');
					$arrAddr[$index]['street'] = $objAddressBook->street;
					$arrAddr[$index]['street_2'] = $objAddressBook->street_2;
					$arrAddr[$index]['postcode'] = $objAddressBook->postcode;
					if ($objAddressBook->country == 'cn')
					$arrAddr[$index]['state'] = $arrStates[$objAddressBook->state];
					else
						$arrAddr[$index]['state'] = $objAddressBook->state;
					$arrAddr[$index]['state_code'] = $objAddressBook->state;
					if ($objAddressBook->country == 'cn')
						$arrAddr[$index]['city'] = $arrCitys[$objAddressBook->city];
					else
						$arrAddr[$index]['city'] = $objAddressBook->city;
					$arrAddr[$index]['city_code'] = $objAddressBook->city;
					$arrAddr[$index]['country'] = $countries[$objAddressBook->country];
					$arrAddr[$index]['id_country'] = $objAddressBook->country;
					$arrAddr[$index]['cellphone'] = $objAddressBook->cellphone;
					$arrAddr[$index]['telephone'] = $objAddressBook->telephone;
					$arrAddr[$index]['default_address'] = intval($objAddressBook->default_address) ? true : false;
					
					$index++;
				}
			}			
			
			// Template of order.
			$strHtml = '<div class="path"><a title="HOME" href="index.php">HOME</a>&nbsp;' . htmlentities(">") . '&nbsp;<a title="MY ACCOUNT" href="profile.html">' . (($objMember->firstname) ? $objMember->firstname : $objMember->email) . '\'s account</a></div><div class="quick_tool">
					<a title="ORDER STATUS" href="order-status.html">ORDER STATUS</a> <a href="profile_detail.html" title="PROFILE">PROFILE</a> <a title="WISHLIST" href="backpocket.html">WISHLIST</a> <a title="ADDRESS BOOK" href="shipping_payment.html" class="active">ADDRESS BOOK</a>
				</div>
				<div id="shipping_payment" class="main">
				<h1>ADDRESS BOOK</h1>
			<script type="text/javascript">
				function state_change()
				{
					jQuery.ajax({
						type: "POST",
						url: \'ajaxCitys.php\',
						data: \'citys=1&id_state=\'+ jQuery("select[@name=\'state_shipping\']").val(),
						success: function(msg){ 
							//alert( "Data Saved: " +   msg ); 
							
							jQuery("select[@name=\'city_shipping\']").html(msg);
						} 
					});
				}
				
				jQuery(function(){
				jQuery("a[@name=\'shipping_edit\']").click(function(){
					/*jQuery.ajax({
						type: "POST",
						url: "shipping_addr.php",
						data: "action=get&id_addr=" + jQuery("input[@name=\'address_shipping\'][@checked]").val(),
						success: function(msg) {alert( "Data Saved: " +   msg );}
					});*/
					
					jQuery("input[@name=\'btn_add_shipping\']").val("SUBMIT");
					jQuery("input[@name=\'firstname_shipping\']").val(jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'firstname_hid\']").val());
					jQuery("input[@name=\'lastname_shipping\']").val(jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'lastname_hid\']").val());
					jQuery("input[@name=\'street_shipping\']").val(jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'street_hid\']").val());
					jQuery("input[@name=\'street_2_shipping\']").val(jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'street_2_hid\']").val());
					jQuery("input[@name=\'postcode_shipping\']").val(jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'postcode_hid\']").val());

					if (jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'country_hid\']").val() == \'cn\')
					{
						// get all the state and city.
						var strState = \'<select class="state mobile" value="" name="state_shipping" onchange="state_change();"><option value=""> - please select -</option>\';';
					
					$strOptionState = "";
					foreach ($arrStates as $key=>$strSate)
					{
						$strOptionState .= "<option value=\"".$key."\">$strSate</option>";
					}
					
					$strHtml .= '
						strState += \'' . $strOptionState . '\';
						strState += "</select>(optional)";
						
						var strCity = \'<select class="state mobile" value="" name="city_shipping"><option value=""> - please select -</option>\';';
					
					$strOptionCity = "";
					foreach ($arrCitys as $key=>$strCity)
					{
						$strOptionCity .= "<option value=\"".$key."\">" . addslashes($strCity) . "</option>";
					}
					$strHtml .= '
						strCity += \'' . $strOptionCity . '\';
						strCity += "</select>";
						
						// city.
						var shipping_html = jQuery("input[@name=\'state_shipping\']").parent().html();
						if (shipping_html != null)
						{
							var objParent = jQuery("input[@name=\'state_shipping\']").parent();
							var strReplace = "";
							//alert(shipping_html);
							if (shipping_html.toLowerCase().indexOf(\'<input\') > -1)
								strReplace = shipping_html.substr(shipping_html.toLowerCase().indexOf(\'<input\'));
							//alert(strReplace);
							shipping_html = shipping_html.replace(strReplace, strState);
							objParent.html(shipping_html);
							
							var shipping_html = jQuery("input[@name=\'city_shipping\']").parent().html();
							var objParent = jQuery("input[@name=\'city_shipping\']").parent();
							var strReplace = "";
							//alert(shipping_html);
							if (shipping_html.toLowerCase().indexOf(\'<input\') > -1)
								strReplace = shipping_html.substr(shipping_html.toLowerCase().indexOf(\'<input\'));
							//alert(strReplace);
							shipping_html = shipping_html.replace(strReplace, strCity);
							objParent.html(shipping_html);
						}
											
						jQuery("select[@name=\'state_shipping\']").val(jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'state_hid\']").val());
						jQuery.ajax({
									type: "POST",
									url: \'ajaxCitys.php\',
									data: \'citys=1&id_state=\'+ jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'state_hid\']").val(),
									success: function(msg){ 
										//alert( "Data Saved: " +   msg ); 
										jQuery("select[@name=\'city_shipping\']").html(msg);					
										jQuery("select[@name=\'city_shipping\']").val(jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'city_hid\']").val());
									} 
								});
					}
					else
					{
						var shipping_html = jQuery("select[@name=\'city_shipping\']").parent().html();
						if (shipping_html != null)
						{
							var objParent = jQuery("select[@name=\'city_shipping\']").parent();
							var re = /<select[^>]*>[\s\S]*?<\/select>/ig;
							shipping_html = shipping_html.replace(re, \'<input type="text" name="city_shipping" value="\'+jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'city_hid\']").val()+\'" class="ce_text mobile" />\');
							objParent.html(shipping_html);
							
							objParent = jQuery("select[@name=\'state_shipping\']").parent();
							shipping_html = jQuery("select[@name=\'state_shipping\']").parent().html();
							if (shipping_html.toLowerCase().indexOf(\'<select\') > -1)
							{
								var re = /<select[^>]*>[\s\S]*?<\/select>/ig;
								shipping_html = shipping_html.replace(re, \'<input type="text" name="state_shipping" value="\'+jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'state_hid\']").val()+\'" class="ce_text mobile" />\');
								objParent.html(shipping_html);
							}
						}else{
							jQuery("input[@name=\'state_shipping\']").val(jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'city_hid\']").val());
							jQuery("input[@name=\'city_shipping\']").val(jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'state_hid\']").val());
						}
					}
					jQuery("select[@name=\'country_shipping\']").val(jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'country_hid\']").val());
					jQuery("input[@name=\'cellphone_shipping\']").val(jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'cellphone_hid\']").val());
					jQuery("input[@name=\'telephone_shipping\']").val(jQuery("input[@name=\'address_shipping\'][@checked]").parent().find("input[@name=\'telephone_hid\']").val());
					jQuery("input[@name=\'default_address\']").attr("checked", "checked");
					jQuery("input[@name=\'shipping_action\']").val("edit");
					
					return false;
				});
				
				jQuery("select[@name=\'country_shipping\']").change(function(){
					if (jQuery(this).val() != \'cn\')
					{
						// city.
						var shipping_html = jQuery("select[@name=\'city_shipping\']").parent().html();
						if (shipping_html != null)
						{
							var objParent = jQuery("select[@name=\'city_shipping\']").parent();
							if (shipping_html.toLowerCase().indexOf(\'<select\') > -1)
							{
								var re = /<select[^>]*>[\s\S]*?<\/select>/ig;
								shipping_html = shipping_html.replace(re, \'<input type="text" name="city_shipping" value="" class="ce_text mobile" />\');
								objParent.html(shipping_html);
							}
							
							// state.
							objParent = jQuery("select[@name=\'state_shipping\']").parent();
							shipping_html = jQuery("select[@name=\'state_shipping\']").parent().html();
							if (shipping_html.toLowerCase().indexOf(\'<select\') > -1)
							{
								var re = /<select[^>]*>[\s\S]*?<\/select>/ig;
								shipping_html = shipping_html.replace(re, \'<input type="text" name="state_shipping" value="" class="ce_text mobile" />\');
								objParent.html(shipping_html);
							}
						}
					}
					else
					{
						// get all the state and city.
						var strState = \'<select class="state mobile" value="" name="state_shipping" onchange="state_change();"><option value=""> - please select -</option>\';';
					
					$strOptionState = "";
					foreach ($arrStates as $key=>$strSate)
					{
						$strOptionState .= "<option value=\"".$key."\">$strSate</option>";
					}
					
					$strHtml .= '
						strState += \'' . $strOptionState . '\';
						strState += "</select>";
						
						var strCity = \'<select class="state mobile" value="" name="city_shipping"><option value=""> - please select -</option>\';';
					
					$strOptionCity = "";
					foreach ($arrCitys as $key=>$strCity)
					{
						$strOptionCity .= "<option value=\"".$key."\">" . addslashes($strCity) . "</option>";
					}
					$strHtml .= '
						strCity += \'' . $strOptionCity . '\';
						strCity += "</select>";
						
						// city.
						var shipping_html = jQuery("input[@name=\'state_shipping\']").parent().html();
						if (shipping_html != null)
						{
							var objParent = jQuery("input[@name=\'state_shipping\']").parent();
							var strReplace = "";
							//alert(shipping_html);
							if (shipping_html.toLowerCase().indexOf(\'<input\') > -1)
								strReplace = shipping_html.substr(shipping_html.toLowerCase().indexOf(\'<input\'));
							//alert(strReplace);
							shipping_html = shipping_html.replace(strReplace, strState);
							objParent.html(shipping_html);
							
							var shipping_html = jQuery("input[@name=\'city_shipping\']").parent().html();
							var objParent = jQuery("input[@name=\'city_shipping\']").parent();
							var strReplace = "";
							//alert(shipping_html);
							if (shipping_html.toLowerCase().indexOf(\'<input\') > -1)
								strReplace = shipping_html.substr(shipping_html.toLowerCase().indexOf(\'<input\'));
							//alert(strReplace);
							shipping_html = shipping_html.replace(strReplace, strCity);
							objParent.html(shipping_html);
						}
					}
					
					return false;
				});
				
				/*jQuery("select[@name=\'country_billing\']").change(function(){
					if (jQuery(this).val() != \'cn\')
					{
						// city.
						var shipping_html = jQuery("select[@name=\'city_billing\']").parent().html();
						if (shipping_html != null)
						{
							var objParent = jQuery("select[@name=\'city_billing\']").parent();
							if (shipping_html.indexOf(\'<select\') > -1)
							{
								var re = /<select[^>]*>[\s\S]*?<\/select>/ig;
								shipping_html = shipping_html.replace(re, \'<input type="text" name="city_billing" value="" class="ce_text mobile" />\');
								objParent.html(shipping_html);
							}
							
							// state.
							objParent = jQuery("select[@name=\'state_billing\']").parent();
							shipping_html = jQuery("select[@name=\'state_billing\']").parent().html();
							if (shipping_html.indexOf(\'<select\') > -1)
							{
								var re = /<select[^>]*>[\s\S]*?<\/select>/ig;
								shipping_html = shipping_html.replace(re, \'<input type="text" name="state_billing" value="" class="ce_text mobile" />\');
								objParent.html(shipping_html);
							}
						}
					}
					else
					{
						// get all the state and city.
						var strState = \'<select class="state mobile" value="" name="state_billing" onchange="state_change();"><option value=""> - please select -</option>\';';
					
					$strOptionState = "";
					foreach ($arrStates as $key=>$strSate)
					{
						$strOptionState .= "<option value=\"".$key."\">$strSate</option>";
					}
					
					$strHtml .= '
						strState += \'' . $strOptionState . '\';
						strState += "</select>";
						
						var strCity = \'<select class="state mobile" value="" name="city_billing"><option value=""> - please select -</option>\';';
					
					$strOptionCity = "";
					foreach ($arrCitys as $key=>$strCity)
					{
						$strOptionCity .= "<option value=\"".$key."\">" . addslashes($strCity) . "</option>";
					}
					$strHtml .= '
						strCity += \'' . $strOptionCity . '\';
						strCity += "</select>";
						
						// city.
						var shipping_html = jQuery("input[@name=\'state_billing\']").parent().html();
						if (shipping_html != null)
						{
							var objParent = jQuery("input[@name=\'state_billing\']").parent();
							var strReplace = "";
							//alert(shipping_html);
							if (shipping_html.indexOf(\'<input\') > -1)
								strReplace = shipping_html.substr(shipping_html.indexOf(\'<input\'));
							//alert(strReplace);
							shipping_html = shipping_html.replace(strReplace, strState);
							objParent.html(shipping_html);
							
							var shipping_html = jQuery("input[@name=\'city_billing\']").parent().html();
							var objParent = jQuery("input[@name=\'city_billing\']").parent();
							var strReplace = "";
							//alert(shipping_html);
							if (shipping_html.indexOf(\'<input\') > -1)
								strReplace = shipping_html.substr(shipping_html.indexOf(\'<input\'));
							//alert(strReplace);
							shipping_html = shipping_html.replace(strReplace, strCity);
							objParent.html(shipping_html);
						}
					}
					
					return false;
				});*/
				
				jQuery("a[@name=\'shipping_remove\']").click(function(){
					
					if (confirm("Are you sure to delete this shipping address?"))
						jQuery.ajax({
								type: "POST",
								url: \'shipping_addr.php\',
								data: \'action=delete&id_addr=\' + jQuery(this).parent().parent().find("input[@name=\'address_shipping\']").val(),
								success: function(msg){ //alert( "Data Saved: " +   msg );
									window.location.href = window.location;
								} 
							});
						
					return false;
				});
				
				jQuery("input[@name=\'postcode_shipping\']").blur(function(el){
					var _target = jQuery(el.target||el);
					if (_target.val() == ""){
						jQuery(".shipping_address .contactus_item").find("label[@for=\'postcode_shipping\']").show();
					}else{
						jQuery(".shipping_address .contactus_item").find("label[@for=\'postcode_shipping\']").hide();
					}
				});
				
				jQuery("input[@name=\'firstname_shipping\']").blur(function(el){
					var _target = jQuery(el.target||el);
					if (_target.val() == ""){
						jQuery(".shipping_address .name_item").find("label[@for=\'firstname_shipping\']").show();
					}else{
						jQuery(".shipping_address .name_item").find("label[@for=\'firstname_shipping\']").hide();
					}
				});
				
				jQuery("input[@name=\'lastname_shipping\']").blur(function(el){
					var _target = jQuery(el.target||el);
					if (_target.val() == ""){
						jQuery(".shipping_address .name_item").find("label[@for=\'lastname_shipping\']").show();
					}else{
						jQuery(".shipping_address .name_item").find("label[@for=\'lastname_shipping\']").hide();
					}
				});
				
				jQuery("input[@name=\'city_shipping\']").blur(function(el){
					var _target = jQuery(el.target||el);
					if (_target.val() == ""){
						jQuery(".shipping_address .contactus_item").find("label[@for=\'city_shipping\']").show();
					}else{
						jQuery(".shipping_address .contactus_item").find("label[@for=\'city_shipping\']").hide();
					}
				});
				
				jQuery("input[@name=\'cellphone_shipping\']").blur(function(el){
					var _target = jQuery(el.target||el);
					if (_target.val() == ""){
						jQuery("#shipping_payment .contactus_item").find("label[@for=\'cellphone_shipping\']").html("Please input your mobile number.");
						jQuery("#shipping_payment .contactus_item").find("label[@for=\'cellphone_shipping\']").show();
					}else{
						if (!/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(jQuery("input[@name=\'cellphone_shipping\']").val()))
						{
							jQuery("#shipping_payment .contactus_item").find("label[@for=\'cellphone_shipping\']").html("Cellphone must be digits.");
							jQuery("#shipping_payment .contactus_item").find("label[@for=\'cellphone_shipping\']").show();
						}
						else
						{
							jQuery("#shipping_payment .contactus_item").find("label[@for=\'cellphone_shipping\']").hide();
						}
					}
				});
				
				jQuery("input[@name=\'btn_add_shipping\']").click(function(){
					var strSate = (jQuery("select[@name=\'country_shipping\']").val() == \'cn\') ? jQuery("select[@name=\'state_shipping\']").val() : jQuery("input[@name=\'state_shipping\']").val();
					var strCity = (jQuery("select[@name=\'country_shipping\']").val() == \'cn\') ? jQuery("select[@name=\'city_shipping\']").val() : jQuery("input[@name=\'city_shipping\']").val();
					
					var is_validate = true;
					var val_firstname = true;
					var val_lastname = true;
					var val_street_1 = true;
					var val_postcode = true;
					var val_country = true;
					var val_state = true;
					var val_city = true;
					var val_mobile = true;
					var var_phone = true;
					
					/**
					 * validate columns
					 */
					/** first shipping address firstname. **/
					if (jQuery("input[@name=\'firstname_shipping\']").val() == "")
					{
						val_firstname = false;
						jQuery(".shipping_address .name_item").find("label[@for=\'firstname_shipping\']").show();
					}
					else
					{
						val_firstname = true;
						jQuery(".shipping_address .name_item").find("label[@for=\'firstname_shipping\']").hide();
					}
					
					// last name.
					if (jQuery("input[@name=\'lastname_shipping\']").val() == "")
					{
						val_lastname = false;
						jQuery(".shipping_address .name_item").find("label[@for=\'lastname_shipping\']").show();
					}
					else
					{
						val_lastname = true;
						jQuery(".shipping_address .name_item").find("label[@for=\'lastname_shipping\']").hide();
					}
					
					// street 1
					if (jQuery("input[@name=\'street_shipping\']").val() == "")
					{
						val_street_1 = false;
						jQuery(".shipping_address .street_address").find("label[@for=\'street_shipping\']").show();
					}
					else
					{
						val_street_1 = true;
						jQuery(".shipping_address .street_address").find("label[@for=\'street_shipping\']").hide();
					}
					
					// post code.
					if (jQuery("input[@name=\'postcode_shipping\']").val() == "")
					{
						val_postcode = false;
						jQuery(".shipping_address .contactus_item").find("label[@for=\'postcode_shipping\']").show();
					}
					else
					{
						/*if (/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(jQuery("input[@name=\'postcode_shipping\']").val()))
						{
							if (jQuery("input[@name=\'postcode_shipping\']").val().length == 6)
							{
								val_postcode = true;
								jQuery(".shipping_address .contactus_item").find("label[@for=\'postcode_shipping\']").hide();
							}
							else
							{
								val_postcode = false;
								jQuery(".shipping_address .contactus_item").find("label[@for=\'postcode_shipping\']").html("The postcode must equal to 6 digits.");
								jQuery(".shipping_address .contactus_item").find("label[@for=\'postcode_shipping\']").show();
							}
						}
						else
						{
							val_postcode = false;
							jQuery(".shipping_address .contactus_item").find("label[@for=\'postcode_shipping\']").html("The postcode must be digits.");
							jQuery(".shipping_address .contactus_item").find("label[@for=\'postcode_shipping\']").show();
						}*/
					}
					
					// country
					if (jQuery("select[@name=\'country_shipping\']").val() == "")
					{
						val_country = false;
						jQuery(".shipping_address .contactus_item").find("label[@for=\'country_shipping\']").show();
					}
					else
					{
						val_country = true;
						jQuery(".shipping_address .contactus_item").find("label[@for=\'country_shipping\']").hide();
					}
					
					// state
					/*if (jQuery("select[@name=\'state_shipping\']").html() != null)
					{
						jQuery(".shipping_address .contactus_item").find("label[@for=\'state_shipping\']").html("Please select state.");
						if (jQuery("select[@name=\'state_shipping\']").val() == "")
						{
							val_state = false;
							jQuery(".shipping_address .contactus_item").find("label[@for=\'state_shipping\']").show();
						}
						else
						{
							val_state = true;
							jQuery(".shipping_address .contactus_item").find("label[@for=\'state_shipping\']").hide();
						}
					}
					else
					{
						jQuery(".shipping_address .contactus_item").find("label[@for=\'state_shipping\']").html("Please input state.");
						if (jQuery("input[@name=\'state_shipping\']").val() == "")
						{
							val_state = false;
							jQuery(".shipping_address .contactus_item").find("label[@for=\'state_shipping\']").show();
						}
						else
						{
							val_state = true;
							jQuery(".shipping_address .contactus_item").find("label[@for=\'state_shipping\']").hide();
						}
					}*/
					
					// city
					if (jQuery("select[@name=\'city_shipping\']").html() != null)
					{
						jQuery(".shipping_address .contactus_item").find("label[@for=\'city_shipping\']").html("Please select city.");
						if (jQuery("select[@name=\'city_shipping\']").val() == "")
						{
							val_city = false;
							jQuery(".shipping_address .contactus_item").find("label[@for=\'city_shipping\']").show();
						}
						else
						{
							val_city = true;
							jQuery(".shipping_address .contactus_item").find("label[@for=\'city_shipping\']").hide();
						}
					}
					else
					{
						jQuery(".shipping_address .contactus_item").find("label[@for=\'city_shipping\']").html("Please input city.");
						if (jQuery("input[@name=\'city_shipping\']").val() == "")
						{
							val_city = false;
							jQuery(".shipping_address .contactus_item").find("label[@for=\'city_shipping\']").show();
						}
						else
						{
							val_city = true;
							jQuery(".shipping_address .contactus_item").find("label[@for=\'city_shipping\']").hide();
						}
					}
					
					// mobile phone
					if (jQuery("input[@name=\'cellphone_shipping\']").val() == "")
					{
						val_mobile = false;
						jQuery(".main_right .contactus_item").find("label[@for=\'cellphone_shipping\']").show();
					}
					else
					{
						if (/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(jQuery("input[@name=\'cellphone_shipping\']").val()))
						{
							val_mobile = true;
							jQuery(".main_right .contactus_item").find("label[@for=\'cellphone_shipping\']").hide();
						}
						else
						{
							val_mobile = false;
							jQuery(".main_right .contactus_item").find("label[@for=\'cellphone_shipping\']").html("Cellphone must be digits.");
							jQuery(".main_right .contactus_item").find("label[@for=\'cellphone_shipping\']").show();
						}
					}
					
					if (jQuery("input[@name=\'telephone_shipping\']").val() != "")
					{
						if (/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(jQuery("input[@name=\'telephone_shipping\']").val()))
						{
							var_phone = true;
							jQuery(".main_right .contactus_item").find("label[@for=\'telephone_shipping\']").hide();
						}
						else
						{
							var_phone = false;
							jQuery(".main_right .contactus_item").find("label[@for=\'telephone_shipping\']").show();
						}
					}
					
					is_validate = (val_firstname && val_lastname && val_street_1 && val_postcode && val_country && val_state && val_city && val_mobile && var_phone) ? true : false;
					
					if (is_validate)
					{					
						if (jQuery("input[@name=\'shipping_action\']").val() == "edit")
						{
							jQuery.ajax({
								type: "POST",
								url: \'shipping_addr.php\',
								data: \'action=\' + jQuery("input[@name=\'shipping_action\']").val() + \'&id_addr=\' + jQuery("input[@name=\'address_shipping\'][@checked]").val() + \'&firstname=\'+ jQuery("input[@name=\'firstname_shipping\']").val() + \'&lastname=\' + jQuery("input[@name=\'lastname_shipping\']").val() + \'&default_address=0&street_shipping=\' + jQuery("input[@name=\'street_shipping\']").val() + \'&street_2_shipping=\' + jQuery("input[@name=\'street_2_shipping\']").val() + \'&postcode_shipping=\' + jQuery("input[@name=\'postcode_shipping\']").val() + \'&city_shipping=\' + strCity + \'&state_shipping=\' + strSate + \'&country_shipping=\' + jQuery("select[@name=\'country_shipping\']").val() + \'&cellphone_shipping=\' + jQuery("input[@name=\'cellphone_shipping\']").val() + \'&telephone_shipping=\' + jQuery("input[@name=\'telephone_shipping\']").val() + \'&gender=\' + jQuery("input[@name=\'gender\']").val(),
								success: function(msg){ //alert( "Data Saved: " +   msg );
									window.location.href = window.location;
								} 
							});
						}
						else
							jQuery.ajax({
								type: "POST",
								url: \'shipping_addr.php\',
								data: \'action=\' + jQuery("input[@name=\'shipping_action\']").val() + \'&firstname=\'+ jQuery("input[@name=\'firstname_shipping\']").val() + \'&lastname=\' + jQuery("input[@name=\'lastname_shipping\']").val() + \'&default_address=0&street_shipping=\' + jQuery("input[@name=\'street_shipping\']").val() + \'&street_2_shipping=\' + jQuery("input[@name=\'street_2_shipping\']").val() + \'&postcode_shipping=\' + jQuery("input[@name=\'postcode_shipping\']").val() + \'&city_shipping=\' + strCity + \'&state_shipping=\' + strSate + \'&country_shipping=\' + jQuery("select[@name=\'country_shipping\']").val() + \'&cellphone_shipping=\' + jQuery("input[@name=\'cellphone_shipping\']").val() + \'&telephone_shipping=\' + jQuery("input[@name=\'telephone_shipping\']").val() + \'&gender=\' + jQuery("input[@name=\'gender\']").val() + \'&email=\' + jQuery("input[@name=\'email_hide\']").val() + \'&uid=\' + jQuery("input[@name=\'uid\']").val() + \'&id_member=\' + jQuery("input[@name=\'uid\']").val(),
								success: function(msg){ 
									//alert( "Data Saved: " +   msg ); 
									window.location.href = window.location;
								} 
							});
					}
					
					return false;
				});
				
				jQuery("select[@name=\'state_shipping\']").change(function(){
						jQuery.ajax({
								type: "POST",
								url: \'ajaxCitys.php\',
								data: \'citys=1&id_state=\'+ jQuery("select[@name=\'state_shipping\']").val(),
								success: function(msg){ 
									//alert( "Data Saved: " +   msg ); 
									
									jQuery("select[@name=\'city_shipping\']").html(msg);
								} 
							});
					});
				
				/*jQuery("select[@name=\'state_billing\']").change(function(){
						jQuery.ajax({
								type: "POST",
								url: \'ajaxCitys.php\',
								data: \'citys=1&id_state=\'+ jQuery("select[@name=\'state_billing\']").val(),
								success: function(msg){ 
									//alert( "Data Saved: " +   msg ); 
									
									jQuery("select[@name=\'city_billing\']").html(msg);
								} 
							});
					});*/
				
				/*jQuery("input[@name=\'btn_add_billing\']").click(function(){
					var state_billing = (jQuery("select[@name=\'country_billing\']").val() == \'cn\') ? jQuery("select[@name=\'state_billing\']").val() : jQuery("input[@name=\'state_billing\']").val();
					var city_billing = (jQuery("select[@name=\'country_billing\']").val() == \'cn\') ? jQuery("select[@name=\'city_billing\']").val() : jQuery("input[@name=\'city_billing\']").val();
					var str_street_2 = jQuery("input[@name=\'street_2\']").val() ? jQuery("input[@name=\'street_2\']").val() : \'\';
					jQuery.ajax({
							type: "POST",
							url: \'billing_addr.php\',
							data: \'action=edit&firstname=\'+ jQuery("input[@name=\'firstname_billing\']").val() + \'&lastname=\' + jQuery("input[@name=\'lastname_billing\']").val() + \'&street_billing=\' + jQuery("input[@name=\'street_billing\']").val() + \'&street_2=\' + str_street_2 + \'&postal_billing=\' + jQuery("input[@name=\'postal_billing\']").val() + \'&city_billing=\' + city_billing + \'&state_billing=\' + state_billing + \'&country_billing=\' + jQuery("select[@name=\'country_billing\']").val() + \'&mobile_billing=\' + jQuery("input[@name=\'mobile_billing\']").val() + \'&phone_billing=\' + jQuery("input[@name=\'phone_billing\']").val() + \'&uid=\' + jQuery("input[@name=\'uid\']").val(),
							success: function(msg){ 
								//alert( "Data Saved: " +   msg ); 
								window.location.href = window.location;
							} 
						});
						
						return false;
				});*/
			});
			</script>
			<form method="post" action="payment.html?step=2" name="shippingform" >
			<div class="main_left">
	<div id="addresses">
	<div class="info">';
			$index = 0;
			foreach ($arrAddr as $arrStrAddr)
			{
				$strHtml .= '<div class="cons">
				<input type="radio" name="address_shipping" value="' . $arrStrAddr['id'] . '"' . (($arrStrAddr['default_address']) ? 'checked="checked"' : '') . (($index == 0) ? 'checked="checked"' : '') . '><p>' . $arrStrAddr['username'] . ', <br/>' . $arrStrAddr['address'] . ', ' . $arrStrAddr['city'] . '<br />' . $arrStrAddr['state'] . ' ' . $arrStrAddr['postcode'] . ' ' . $arrStrAddr['country'] . '</p><p class="tool"><a href="#" title="edit shipping address" name="shipping_edit" class="edit">edit</a> <a href="#" title="delete shipping address" name="shipping_remove" class="remove">remove</a></p>
				<!-- input hidden area -->
				<input type="hidden" name="firstname_hid" value="' . $arrStrAddr['firstname'] . '" />
				<input type="hidden" name="lastname_hid" value="' . $arrStrAddr['lastname'] . '" />
				<input type="hidden" name="street_hid" value="' . $arrStrAddr['street'] . '" />
				<input type="hidden" name="street_2_hid" value="' . $arrStrAddr['street_2'] . '" />
				<input type="hidden" name="postcode_hid" value="' . $arrStrAddr['postcode'] . '" />
				<input type="hidden" name="city_hid" value="' . $arrStrAddr['city_code'] . '" />
				<input type="hidden" name="state_hid" value="' . $arrStrAddr['state_code'] . '" />
				<input type="hidden" name="country_hid" value="' . $arrStrAddr['id_country'] . '" />
				<input type="hidden" name="cellphone_hid" value="' . $arrStrAddr['cellphone'] . '" />
				<input type="hidden" name="telephone_hid" value="' . $arrStrAddr['telephone'] . '" />
				<input type="hidden" name="gender" value="' . $objMember->gender . '" />
				<input type="hidden" name="email_hide" value="' . $objMember->email . '" />
				<input type="hidden" name="uid" value="' . $objMember->id . '" />
			</div>';
			
				$index++;
			}
		
	$strHtml .= '<input type="hidden" name="shipping_action" value="add" /></div>

	</div>  <!-- addresses end -->
	</div>

	<div class="main_right">
		<div class="shipping_address">	
		<div class="name_item">
			<p>
				<label>FIRST NAME</label><input type="text" name="firstname_shipping" value="" class="ce_text" />
				<label for="firstname_shipping" class="error" style="display:none;">Please input your first name.</label>
				<label>LAST NAME</label><input type="text" name="lastname_shipping" value="" class="ce_text" />
				<label for="lastname_shipping" class="error" style="display:none;">Please input your last name.</label>
			</p>
		</div>
		<div class="street_address">
			<!--<p>
				<label>STREET ADDRESS</label><input type="checkbox" name="default_address" value="1" class="checkbox" /> <span class="notice">make this default shipping address</span>
			</p>-->
			<p>
				<label>ADDRESS</label>
				<input type="text" name="street_shipping" value="Address line 1" onfocus="if (this.value == \'Address line 1\') {this.value = \'\';}" onblur="if (this.value == \'\'){this.value = \'Address line 1\'; }" class="ce_text address_text" />
				<label for="street_shipping" class="error" style="display:none;">Please input your address line 1.</label>
				<input type="text" name="street_2_shipping" value="Address line 2" onfocus="if (this.value == \'Address line 2\') {this.value = \'\';}" onblur="if (this.value == \'\'){this.value = \'Address line 2\'; }" class="ce_text address_text" />
			</p>	
		</div>
		
		<div class="contactus_item">
			<p>
				<label>Zipcode</label><input type="text" name="postcode_shipping" value="" class="ce_text mobile" />
				<label for="postcode_shipping" class="error" style="display:none;">Please input your post code.</label>
			</p>
			<p>
			<label>Country</label><select class="country mobile" name="country_shipping">
									<option value="" selected="selected"> - please select -</option>';
		
			foreach ($countries as $key=>$strCountry)
			{
				if ($key == 'us')
					$strHtml .= '<option value="'.$key.'" selected="selected">'.$strCountry.'</option>';
				else
					$strHtml .= '<option value="'.$key.'">'.$strCountry.'</option>';
			}
			
			$strHtml .= '</select><label for="country_shipping" class="error" style="display:none;">Please select country.</label>
		</p>
		<p>
			<label>State</label><input type="text" class="ce_text mobile" value="" name="state_shipping">(optional)';		
			
			$strHtml .= '<label for="state_shipping" class="error" style="display:none;">Please select state.</label></p>
		<p>
			<label>City</label><input type="text" class="ce_text mobile" value="" name="city_shipping">';		
			$strHtml .= '<label for="city_shipping" class="error" style="display:none;">Please select country.</label>
		</p>			
	</div>	
			
		</div>		
		
		<div class="contactus_item">
			<p>
				<label>MOBILE</label><input type="text" name="cellphone_shipping" value="" class="ce_text mobile" />
				<label for="cellphone_shipping" class="error" style="display:none;">Please input your mobile number.</label>
			</p>
			<p>
				<label>TELEPHONE</label><input type="text" name="telephone_shipping" value="" class="ce_text mobile" />
				<label for="telephone_shipping" class="error" style="display:none;">Phone number must be digits.</label>
			</p>
		</div>
		<p class="submits"><input type="button" name="btn_add_shipping" value="ADD ADDRESS" class="submit" /></p>
	</div>
		</form> <!-- form end -->
		</div>';
		
		}
		else
		{
			$this->redirect("sign-in.html");
		}
		
		return $strHtml;
	}
}
?>